﻿using BussinessObject.ObjectTwo;
using Class_Entity.ObjectTwo;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ProjectHK3.App_Code;
using System.IO;
namespace ProjectHK3.Object_One
{
    public partial class EmpoyeeDetail : System.Web.UI.Page
    {
        public static bool anh_Status;
        public static String anh_fie;
        public static String tam;
        B_GetAccount ex = new B_GetAccount();
        Account fx = new Account();
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                if (!IsPostBack)
                {                   
                    loadData();
                }
            }
            catch (Exception ex)
            {
                Response.Redirect("../Object One/Login.aspx");
            }    
        }
        public void loadData()
        {
            fx = ex.getvalAccountID(int.Parse(Session["Account_id"].ToString()));
            TextBox1.Text = fx.USER_NAME;
            TextBox2.Text = fx.FIRST_NAME;
            TextBox3.Text = fx.LAST_NAME;
            Image18.ImageUrl = fx.PICTURE;
            TextBox7.Text = fx.BIRTH_DATE;
            TextBox5.Text = fx.PHONE_NUMBER;
            TextBox6.Text = fx.EMAIL;            
            TextBox9.Text = fx.ADDRESS;
            tam = fx.PICTURE;
        }
        public void uploadPic()
        {
            String[] valids = { "bmp", "gif", "PNG", "jpg", "jpeg", "png", "BMP", "GIF", "JPEG", "JPG", "PNG" };
            bool isvalids = false;
            if (FileUpload1.HasFile)
            {
                try
                {
                    String txx = Path.GetExtension(FileUpload1.PostedFile.FileName);

                    for (int i = 0; i < valids.Length; i++)
                    {
                        if (txx == "." + valids[i])
                        {
                            isvalids = true;
                            break;
                        }
                    }
                    if (isvalids == true)
                    {
                        if (FileUpload1.PostedFile.ContentLength < 9002400)
                        {
                            String filename = Path.GetFileName(FileUpload1.FileName);
                            anh_fie = "~/Object One/Pic/Ava/" + filename;
                            FileUpload1.SaveAs(Server.MapPath("/Object One/Pic/Ava/") + filename);
                            anh_Status = true;
                        }
                        else
                            Literal1.Text = "upload Status: the file has to be less than";
                    }
                    else
                        Literal1.Text = "Only image";
                }
                catch (Exception ex)
                {
                    Literal1.Text = "cannot up load";
                }
            }
        }
        public void update(String file)
        {
            fx.USER_NAME = TextBox1.Text.ToString();
            fx.FIRST_NAME = TextBox2.Text.ToString();
            fx.LAST_NAME = TextBox3.Text.ToString();
            fx.PICTURE = file;
            fx.BIRTH_DATE = TextBox7.Text.ToString(); ;
            fx.PHONE_NUMBER = TextBox5.Text.ToString();
            fx.EMAIL = TextBox6.Text.ToString();
            fx.ADDRESS = TextBox9.Text.ToString();
            ex.UpdateAccount(fx);
            Alert.Show("Update Successful");
            loadData();
        }
        protected void TextBox7_TextChanged(object sender, EventArgs e)
        {

        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            uploadPic();
            String anh_tam = "";
            if (anh_Status == false)
            {
                anh_tam = tam;
                update(anh_tam);
            }
            else
            {
                anh_tam = anh_fie;
                update(anh_tam);

            }
        }

        protected void Button2_Click(object sender, EventArgs e)
        {
            Session["name"] = TextBox1.Text.ToString();
            Response.Redirect("../Object One/ChangePasswordForAmin.aspx");
        }

        protected void Button3_Click(object sender, EventArgs e)
        {
            Response.Redirect("../Object One/FindAccount.aspx");
        }
    }
}